﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.CMS.Content.Services;
using Kooboo.CMS.Sites.Persistence;
using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Sites.Models;

namespace Kooboo.CMS.SchemaSyncWrapper
{
    public class SchemaManagerWrapper : SchemaManager
    {
        ISiteProvider _siteProvider;
        SchemaManager _schemaManager;
        public SchemaManagerWrapper(ISiteProvider siteProvider, SchemaManager schemaManager)
        {
            this._siteProvider = siteProvider;
            this._schemaManager = schemaManager;
        }
        private void DoSync(Site site, Repository originalRepository, Action<Repository> syncAction)
        {
            var siteRepository = site.GetRepository();
            if (siteRepository != originalRepository)
            {
                syncAction(siteRepository);
            }
            foreach (var sub in _siteProvider.ChildSites(site))
            {
                DoSync(sub, originalRepository, syncAction);
            }
        }
        public override void ResetForm(Content.Models.Repository repository, string schemaName, Content.Models.FormType formType)
        {
            _schemaManager.ResetForm(repository, schemaName, formType);
            DoSync(Site.Current, repository, delegate(Repository target)
            {
                _schemaManager.ResetForm(target, schemaName, formType);
            });
        }
    }
}
